package PAPUpdated;

import java.io.File;
import java.util.ArrayList;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;

public class PBMAlgorithm {
	/**
	 * CLASSE APENAS PARA TESTAR O ALGORITMO PBM
	 * @param args
	 */
	public static void main(String[] args) {
		PBMAlgorithm pap = new PBMAlgorithm();
		try {
			OWLOntology ontology = pap.readOWL("src/ontologies/Semiport.owl");
			System.out.println("Numero de Classes: " + ontology.getClassesInSignature().size());
			
//			for (OWLClass cls : pap.findRoots(ontology)) {
//				System.out.println(cls.getIRI().getFragment());
//			}
			
//			int count = 0;
//			for (OWLClass cls : ontology.getClassesInSignature()) {
//				if(cls.toString().equals("<http://swrc.ontoware.org/ontology/portal#Manual>")){
//					System.out.println(count);
//				}
//				count++;
//			}
//			System.out.println("___________________________________________________");
			
			Object[] arryOfClass = ontology.getClassesInSignature().toArray();
			
			Partitioning partitioning = new Partitioning(ontology, 0.8, 0.3);
			ArrayList<Block> listOfblocks = partitioning.doPartitioning(ontology.getClassesInSignature(), 4);
			
			for (Block block : listOfblocks) {
				for (OWLClass cls : block.getListOfClasses()) {
					System.out.println(cls.getIRI().getFragment());
				}
				System.out.println("___________________________________________________");
			}
			
//			SUMA DAS CLASSES APOS A BLOCAGEM (VALIDAR)			
			int sum = 0;
			for (Block block : listOfblocks) {
				sum += block.getListOfClasses().size();
			}
			System.out.println(sum);
			//System.out.println(partitioning.calculateLink((OWLClass)arryOfClass[32], (OWLClass)arryOfClass[23]));
			//System.out.println(((OWLClass)arryOfClass[39]).getSuperClasses(ontology).iterator().next().toString());
			
			
			//pap.getClassesOfOntology(ontology);
		} catch (OWLOntologyCreationException e) {
			e.printStackTrace();
		}

	}
		
	/**
	 * Method to read owl file.
	 * @param path
	 * @return OWL Ontology
	 * @throws OWLOntologyCreationException
	 */
	private OWLOntology readOWL(String path) throws OWLOntologyCreationException {
		String DOCUMENT_IRI = path;
		File file = new File(DOCUMENT_IRI);
		// Create our ontology manager in the usual way.
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
		// Load a copy of the people+pets ontology. We'll load the ontology from
		// the web (it's acutally located in the TONES ontology repository).
		IRI docIRI = IRI.create(file);
		// We load the ontology from a document - our IRI points to it directly
		OWLOntology ontology = manager.loadOntologyFromOntologyDocument(docIRI);
		
		return ontology;
	}
	

	
	private ArrayList<OWLClass> findRoots(OWLOntology ontology){
		ArrayList<OWLClass> listOfRoots = new ArrayList<OWLClass>();
		for (OWLClass classe : ontology.getClassesInSignature()) {
			if(classe.getSuperClasses(ontology).isEmpty()){
				listOfRoots.add(classe);
				//System.out.println("E: " + classe.getIRI().getFragment());
			}
			//System.out.println("C: " + classe.getIRI().getFragment() + " - " + classe.getSuperClasses(ontology).isEmpty());
		}
		return listOfRoots;
	}
	
	private void getClassesOfOntology(OWLOntology ontology) {
		for (OWLClass cls : ontology.getClassesInSignature()) {
			System.out.println("Principal Class: " + cls);
			for (OWLClassExpression subclass : cls.getSubClasses(ontology)) {
				if (!subclass.equals(cls)) {
					System.out.println("      Relation: " + subclass);
				}
			}
		}
	}

}
